Utils.Short = {};
Utils.Short.title = null;
Utils.Short.link = null;

Utils.Short.Back = function(LongUrl, callback)
{
    switch(config.url_shortener)
    {
        case '3.ly':
            LongUrl = encodeURIComponent(LongUrl);
            this.Threely(LongUrl, null, null, null, null, callback);
            break;
        case 'migre.me':
            LongUrl = encodeURIComponent(LongUrl);
            this.MigreMe(LongUrl, null, null, null, callback);
            break;
        case 'goo.gl':
            this.GooGl(LongUrl, null, null, null, callback);
            break;
        case 'bit.ly':
            this.BitLy(LongUrl, null, null, null, null, callback);
            break;
        case 'j.mp':
            this.BitLy(LongUrl, null, null, true, null, callback);
            break;
        case 'is.gd':
            LongUrl = encodeURIComponent(LongUrl);
            this.Threely(LongUrl, null, null, 'isgd', null, callback);
            break;
        case 'tr.im':
            LongUrl = encodeURIComponent(LongUrl);
            this.Threely(LongUrl, null, null, 'trim', null, callback);
            break;
        case 'urli':
            this.Threely(LongUrl, null, null, 'urli', null, callback);
            break;
        case 'miudin':
            this.Threely(LongUrl, null, null, 'miudin', null, callback);
            break;
        default:
            LongUrl = encodeURIComponent(LongUrl);
            this.Threely(LongUrl, null, null, null, null, callback);
    }
}

Utils.Short.short = function(LongUrl, tid, tm, cb)
{
    switch(config.url_shortener){
        case '3.ly':
            LongUrl = encodeURIComponent(LongUrl);
            this.Threely(LongUrl, tid, tm, null, cb);
            break;
        case 'migre.me':
            LongUrl = encodeURIComponent(LongUrl);
            this.MigreMe(LongUrl, tid, tm, cb);
            break;
        case 'goo.gl':
            this.GooGl(LongUrl, tid, tm, cb);
            break;
        case 'bit.ly':
            this.BitLy(LongUrl, tid, tm, null, cb);
            break;
        case 'j.mp':
            this.BitLy(LongUrl, tid, tm, true, cb);
            break;
        case 'is.gd':
            LongUrl = encodeURIComponent(LongUrl);
            this.Threely(LongUrl, tid, tm, 'isgd', cb);
            break;
        case 'tr.im':
            LongUrl = encodeURIComponent(LongUrl);
            this.Threely(LongUrl, tid, tm, 'trim', cb);
            break;
        case 'urli':
            this.Threely(LongUrl, tid, tm, 'urli', cb);
            break;
        case 'miudin':
            this.Threely(LongUrl, tid, tm, 'miudin', cb);
            break;
        default:
            LongUrl = encodeURIComponent(LongUrl);
            this.Threely(LongUrl, tid, tm, null, cb);
    }
}

Utils.Short.GooGl = function(LongUrl, tid, tm, callback, bback)
{
  console.log('Shorting URL: '+LongUrl+' With: Goo.gl');
  this.Auth = function(f){function k(){for(var c=0,b=0;b<arguments.length;b++)c=c+arguments[b]&4294967295;return c}function m(c){c=c=String(c>0?c:c+4294967296);var b;b=c;for(var d=0,i=false,j=b.length-1;j>=0;--j){var g=Number(b.charAt(j));if(i){g*=2;d+=Math.floor(g/10)+g%10}else d+=g;i=!i}b=b=d%10;d=0;if(b!=0){d=10-b;if(c.length%2==1){if(d%2==1)d+=9;d/=2}}b=String(d);b+=c;return b}function n(c){for(var b=5381,d=0;d<c.length;d++)b=k(b<<5,b,c.charCodeAt(d));return b}function o(c){for(var b=0,d=0;d<c.length;d++)b=k(c.charCodeAt(d),b<<6,b<<16,-b);return b}f={byteArray_:f,charCodeAt:function(c){return this.byteArray_[c]}};f.length=f.byteArray_.length;var e=n(f.byteArray_);e=e>>2&1073741823;e=e>>4&67108800|e&63;e=e>>4&4193280|e&1023;e=e>>4&245760|e&16383;var l="7";f=o(f.byteArray_);var h=(e>>2&15)<<4|f&15;h|=(e>>6&15)<<12|(f>>8&15)<<8;h|=(e>>10&15)<<20|(f>>16&15)<<16;h|=(e>>14&15)<<28|(f>>24&15)<<24;l+=m(h);return l};
  var shorted;
  var Goo = new Request(api.googl, 'post');
      Goo.dados = {
          user:'toolbar@google.com',
          url: LongUrl,
          auth_token: this.Auth(LongUrl)
      },
      Goo.sucesso = function(re){
          var url = re.short_url;
          Utils.Short.link = url;
          shorted = url;
          console.log(url);
          if(bback){
            bback(url);
            return;
          }
          if(!callback){
              Popup().ShortLinkOK(tid, tm, url);
          }else{
              console.log('Popup().'+callback+'(\''+tid+'\', \''+tm+'\', \''+url+'\')');
              eval('Popup().'+callback+'(\''+tid+'\', \''+tm+'\', \''+url+'\')');
          }
      };
      Goo.erro = function(e){
          console.log('Erro no Goo.gl');
          console.log(e.responseText);
          Popup().ShortLinkOK(tid, tm, '');
      };
      Goo.init();
  return shorted;
}

Utils.Short.BitLy = function(LongUrl, tid, tm, jmp, callback, bback)
{
  console.log('Shorting URL: '+LongUrl+' With: BitLy');
  var shorted;
  var ap = jmp ? api.jmp_short : api.bitly_short;
  var Bit = new Request(ap, 'get', 'json');
      Bit.dados = {
        longUrl: LongUrl,
        apiKey: config.bitly_key,
        login: config.bitly_user,
        version: '2.0.1'
      };
      Bit.sucesso = function(re){
          var url = re.results[LongUrl].shortUrl;
          url = url ? url : '';
          Utils.Short.link = url;
          shorted = url;
          console.log(url);
          if(bback){
            bback(url);
            return;
          }
          if(!callback){
              Popup().ShortLinkOK(tid, tm, url);
          }else{
              console.log('Popup().'+callback+'(\''+tid+'\', \''+tm+'\', \''+url+'\')');
              eval('Popup().'+callback+'(\''+tid+'\', \''+tm+'\', \''+url+'\')');
          }
      };
      Bit.erro = function(e){
          console.log('Erro no BitLy');
          console.log(e.responseText);
          Popup().ShortLinkOK(tid, tm, '');
      };
      Bit.init();
  return shorted;
},

Utils.Short.MigreMe = function(LongUrl, tid, tm, callback, bback)
{
  console.log('Shorting URL: '+LongUrl+' With: Migre.me');
  var shorted;
  var Mi = new Request(api.migre_short, 'get', 'xml');
      Mi.dados = {url: LongUrl};
      Mi.sucesso = function(re){
          var url = $('item > migre', $(re)).text();
          Utils.Short.link = url;
          shorted = url;
          console.log(url);
          if(bback){
            bback(url);
            return;
          }
          if(!callback){
              Popup().ShortLinkOK(tid, tm, url);
          }else{
              console.log('Popup().'+callback+'(\''+tid+'\', \''+tm+'\', \''+url+'\')');
              eval('Popup().'+callback+'(\''+tid+'\', \''+tm+'\', \''+url+'\')');
          }
      };
      Mi.erro = function(e){
          console.log('Erro no Migre.me');
          console.log(e.responseText);
          Popup().ShortLinkOK(tid, tm, '');
      };
      Mi.init();
  return shorted;
}

Utils.Short.Threely = function(LongUrl, tid, tm, other, callback, bback)
{
  console.log('Shorting URL: '+LongUrl+' With: 3.ly/is.gd');
  var shorted;
  var ap,d;
  switch(other){
      case 'isgd':
          console.log('Threely > is.gd');
          ap = api.isgd;
          d  = {longurl: LongUrl};
          break;
      case 'trim':
          console.log('Threely > tr.im');
          ap = api.trim;
          d  = {url: LongUrl};
          break;
      case 'urli':
         console.log('Threely > URLi');
         ap  = api.urli;
         d   = {format: 'simple', action: 'shorturl', url: LongUrl};
         break;
      case 'miudin':
         console.log('Threely > Miud.in');
         ap  = api.miudin;
         //LongUrl = unescape(LongUrl);
         d   = {url: LongUrl};
         break;
      default:
          console.log('Threely > 3.ly');
          ap = api.threely;
          d  = {api: 'em5893833', u: LongUrl};
  }
  var Ly = new Request(ap, 'get', 'text');
      Ly.dados = d;
      Ly.sucesso = function(url){
          Utils.Short.link = url;
          shorted = url;
          console.log(url);
          if(bback){
            bback(url);
            return;
          }
          if(!callback){
              Popup().ShortLinkOK(tid, tm, url);
          }else{
              console.log('Popup().'+callback+'(\''+tid+'\', \''+tm+'\', \''+url+'\')');
              eval('Popup().'+callback+'(\''+tid+'\', \''+tm+'\', \''+url+'\')');
          }
      };
      Ly.erro = function(e){
          console.log('Erro no 3.ly/is.gd');
          console.log(e.responseText);
          Popup().ShortLinkOK(tid, tm, '');
      };
      Ly.init();
}